if(!window.pimpas)
	pimpas = window.pimpas = [];

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}
	
pimpas.Extends = function(Object1, Object2) {
	for(var property in Object2)
		Object1[property] = Object2[property];
}
pimpas.Object = Class.create( );

pimpas.Extends(pimpas, 
	   {
		   IncludeJS: function(src, pimpasSource) {
				var headers = document.getElementsByTagName("head");
				if(headers.length == 0)
					throw "No headers Found";
				
				var header = headers[0];
				if(pimpasSource == null || pimpasSource == undefined) pimpasSource = true;
				
				var script = document.createElement("SCRIPT");
				script.setAttribute("type", "text/javascript");
				
				if(pimpasSource) {
					var pimpasScript = pimpas.FindJS( 'pimpas.js' );
					script.setAttribute("src", pimpasScript.src.replace("pimpas.js", src));
					pimpas.insertAfter(pimpasScript.parentNode, script, pimpasScript);
				} else
					pimpas.insertAfter(header, script, null);
		   },
		   FindJS: function(src) {
				var headers = document.getElementsByTagName("head");
				if(headers.length == 0)
					throw "No headers Found";
				
				var header = headers[0];
				for(var i=0;i<header.childNodes.length;i++)
					if(header.childNodes[i].tagName == 'SCRIPT')
						if(header.childNodes[i].src.endsWith(src)) return header.childNodes[i];
				return null;
		   },
		   RequireJS: function(src) {
			   var script = pimpas.FindJS(src);
			   if(script == null)
					pimpas.IncludeJS( src );
		   },
		   insertAfter: function(parent, newNode, refNode) {
			   var newRefNode = null;
			   if(refNode != null)
					for(var i=0;i<parent.childNodes.length;i++) {
						if(parent.childNodes[i] == refNode) {
							if(parent.childNodes[i+1] != null) {
								newRefNode = parent.childNodes[i+1];
								break;
							}
						}
					}
				if(newRefNode != null)
					parent.insertBefore(newNode, newRefNode);
				else
					parent.appendChild(newNode);
		   }
	   }
);

pimpas.Extends(pimpas.Object,
   {
	   id: null,
	   log: null,
	   initialize: function() {
	    	this.id = pimpas.globalID++;
			this.log = new pimpas.Log();
	   }
   }
);

pimpas.Extends(Function.prototype,	 {
			   bind: function() {
    				var method = this;
					var args = new Array();
					for(var i=0;i<arguments.length;i++)
						args.push(arguments[i]);
					var object = args.shift();
					return function() {
						return method.apply(object, args);
					};
			   }
		}
);
pimpas.Extends(pimpas, {
			   		emptyFunction: function(){},
					globalID: 0,
					scriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)',
					version: 1.0
			   }
);
pimpas.Extends(Array.prototype,
	{
		have: function(value) {
			for(var i=0;i<this.length;i++)
				if(this[i] == value) return true;	
			return false;
		},
		clear: function() {
			while(this.length>0) this.pop( );
		},
		remove: function(value) {
			if(!this.have(value)) return;
			var a = new Array();
			for(var i=0;i<this.length;i++)
				if(this[i] != value) a.push(this[i]);
			this.clear( );
			while(a.length>0) this.push(a.pop());
			return value;
		}
	}
);

pimpas.Extends(String.prototype,
	{
	  stripTags: function() {
		return this.replace(/<\/?[^>]+>/gi, '');
	  },
	  stripScripts: function() {
		return this.replace(new RegExp(pimpas.scriptFragment, 'img'), '');
	  },
	  extractScripts: function() {
		var matchAll = new RegExp(pimpas.scriptFragment, 'img');
		var matchOne = new RegExp(pimpas.scriptFragment, 'im');
		
		var result = (this.match(matchAll) || []);
		for(var i=0;i<result.length;i++)
			result[i] = result[i].stripTags();
		
		return result;
	  },
	  evalScripts: function() {
		var elements = this.extractScripts();
		for(var i=0;i<elements.length;i++)
			eval(elements[i]);
	  },
	  escapeHTML: function() {
		var div = pimpas.DOM.createElement('div', null);
		var text = pimpas.DOM.createTextNode(this);
		pimpas.DOM.add(div, text);
		return this.getObjectValue(div);
	  },
	  unescapeHTML: function() {
		var div = pimpas.DOM.createElement('div', null);
		div.innerHTML = this.stripTags();
		return div.childNodes[0] ? div.childNodes[0].nodeValue : '';
	  },
	  toArray: function() {
		return this.split('');
	  },
	  camelize: function() {
		var oStringList = this.split('-');
		if (oStringList.length == 1) return oStringList[0];
		  
		var camelizedString = this.indexOf('-') == 0
		  ? oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) 
		  : oStringList[0];
		  
		for (var i = 1, len = oStringList.length; i < len; i++) {
		  var s = oStringList[i];
		  camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
		}
		return camelizedString;
	  },
	  inspect: function() {
		return "'" + this.replace('\\', '\\\\').replace("'", '\\\'') + "'";
	  },
	  endsWith: function(cmp) {
		if(this.indexOf(cmp) < 0) return false;
		var posx = this.indexOf(cmp);
		return this.substring(posx) == cmp;
	  },
	  startsWith: function(cmp) {
		if(this.indexOf(cmp)<0) return false;
		var posx = this.indexOf(cmp);
		return this.substring(0, posx) == cmp;
	  }
	}
);